import { BufferGeometry, BufferGeometryLoader, InstancedBufferGeometry } from "../../thirdparty/three/imports";
import { BufferGeometry3D } from "../core/BufferGeometry3D";

export class BufferGeometryLoader3D
{
    _loader = new BufferGeometryLoader();

    setPath(path: string)
    {
        this._loader.setPath(path);

        return this;
    }

    load(url: string, onLoad: (bufferGeometry: BufferGeometry3D) => void, onProgress?: (request: ProgressEvent<EventTarget>) => void, onError?: (event: ErrorEvent) => void)
    {
        const loader = this._loader;
        loader.load(url, (bufferGeometry: InstancedBufferGeometry | BufferGeometry) =>
        {
            if (onLoad)
            {
                if (bufferGeometry.constructor === BufferGeometry)
                {
                    const geometry = BufferGeometry3D.get(bufferGeometry);
                    onLoad(geometry as any);
                }
                else
                {
                    throw "未处理 InstancedBufferGeometry";
                }
            }
        }, onProgress, onError);
    }
}
